package tenumn.energytech.registrys.item;

import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.EnergyStorage;
import org.jetbrains.annotations.Nullable;
import tenumn.energytech.registrys.capability.EnergyCapabilityProvider;

import javax.annotation.Nonnull;

public class ItemChargedCrystal extends ItemEnergy{
    public ItemChargedCrystal(Properties pProperties) {
        super(pProperties);
    }

    @Nullable
    @Override
    public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundTag nbt) {
        return new EnergyCapabilityProvider(()-> new EnergyStorage(getMaxEnergyStorage(),0,Integer.MAX_VALUE, (int) (Math.random()*getMaxEnergyStorage())));
    }
    ItemStack empty;
    public ItemStack newEmpty(){
        if(empty==null) {
            empty = new ItemStack(this);
            empty.getCapability(CapabilityEnergy.ENERGY).ifPresent(iEnergyStorage -> {
                iEnergyStorage.extractEnergy(iEnergyStorage.getEnergyStored(), false);
            });
        }
        return empty;
    }
    @Override
    public int getMaxEnergyStorage() {
        return 114514191;
    }

    @Override
    public int getStateCount() {
        return 0;
    }

    @Override
    public void fillItemCategory(@Nonnull CreativeModeTab pCategory, @Nonnull NonNullList<ItemStack> pItems) {
        if(allowdedIn(pCategory))pItems.add(newEmpty());
    }
}
